const cloudHelper = require('../../../config/project_helpers_config').cloudHelper;
const pageHelper = require('../../../config/project_helpers_config').pageHelper;
const projectBranchBiz = require('../../../config/path_resolver.js').pageHelper;
const validate = require('../../../config/path_resolver.js').pageHelper;
const setting = require('../../../config/path_resolver.js').getRootConfigPath('path_resolver.js').getRootConfigPath('projects.js');
const { logEvent } = require('../../../../helpers/log_helper.js');
const { getNetworkStatus, listenNetworkStatus } = require('../../../../helpers/network_helper.js');

Page({
    data: {
        isLoad: false,
        projectId: '',
        formData: {
            name: '',
            desc: ''
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function(options) {
        if (!options || !options.projectId) {
            pageHelper.showModal('参数错误', '缺少projectId参数');
            setTimeout(() => {
                wx.navigateBack();
            }, 1500);
            return;
        }

        this.setData({
            isLoad: true,
            projectId: options.projectId
        });

        // 网络检测
        const net = await getNetworkStatus();
        if (!net.isConnected) {
            wx.showToast({ title: '当前无网络，请检查连接', icon: 'none' });
        } else if (net.networkType === '2g') {
            wx.showToast({ title: '当前为2G弱网，体验可能受影响', icon: 'none' });
        }
        listenNetworkStatus((res) => {
            if (!res.isConnected) wx.showToast({ title: '网络已断开', icon: 'none' });
            else if (res.networkType === '2g') wx.showToast({ title: '当前为2G弱网', icon: 'none' });
        });
        logEvent('page_load', { page: 'plm_branch_create' });
    },

    /**
     * 表单输入事件
     */
    bindFormChange: function (e) {
        let field = e.currentTarget.dataset.field;
        let val = e.detail.value;
        this.setData({
            [`formData.${field}`]: val
        });
    },

    /**
     * 创建分支
     */
    async bindFormSubmit() {
        let data = this.data;

        // 数据校验
        if (!validate.isTextNotEmpty(data.formData.name)) {
            pageHelper.showModal('分支名称不能为空');
            return;
        }

        if (data.formData.name.length > 20) {
            pageHelper.showModal('分支名称不能超过20个字符');
            return;
        }

        let formData = {
            projectId: data.projectId,
            name: data.formData.name,
            desc: data.formData.desc || ''
        };

        try {
            pageHelper.showLoading('提交中...');
            await projectBranchBiz.createBranch(formData);
            pageHelper.showSuccToast('创建成功', 1500);

            setTimeout(() => {
                let pages = getCurrentPages();
                let prevPage = pages[pages.length - 2];
                prevPage.setData({
                    _reload: true
                });
                wx.navigateBack();
            }, 1500);
        } catch (err) {
            console.error(err);
            pageHelper.showModal('创建失败: ' + err.message);
        }
    }
}) 